package com.chinatsp.onlineapp.ui.weather;

import java.util.ArrayList;
import java.util.List;

import tsp.cn.http.tools.LogUtil;
import tsp.cn.http.tools.PreferenceUtils;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.base.BaseActivity;
import com.chinatsp.onlineapp.base.Constant;
import com.chinatsp.onlineapp.bean.WeatherService;
import com.chinatsp.onlineapp.presenter.WeatherServiceDao;

public class WeatherSinglePageActivity extends BaseActivity {

	private Context mContext;
	private ViewPager vp_weather;
	private LinearLayout ll_point;
	private List<ImageView> list = new ArrayList<ImageView>();
	private ImageView iv1;
	private int CityNum;
	private LinearLayout.LayoutParams layoutParams;
	private int Rcode = 1;
	private String weatherDefaultCity;
	private String weather_city1 = "#";
	private String weather_city2 = "#";
	private List list_cityName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.activity_single_page);
		LogUtil.e("onCreate");
		init();

		// iv1 = (ImageView) findViewById(R.id.iv1);
		// AnimationDrawable anim = (AnimationDrawable) iv1.getBackground();
		// anim.start();

	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		init();
		
	}

	private void init() {
		getData();
		initView();

	}

	private void getData() {
		mContext = this;
		weatherDefaultCity = PreferenceUtils.getPreference(Constant.DEFAULT_CITY,weatherDefaultCity);
		weather_city1 = PreferenceUtils.getPreference(Constant.WEATHER_CITY1, weather_city1);
		weather_city2 = PreferenceUtils.getPreference(Constant.WEATHER_CITY2, weather_city2);
		list_cityName = new ArrayList<String>();
		list_cityName.clear();
		CityNum = 1;
		list_cityName.add(weatherDefaultCity);
		if (!weather_city1.equals("#")) {
			CityNum ++;
			list_cityName.add(weather_city1);
		}
		if (!weather_city2.equals("#")) {
			CityNum ++;
			list_cityName.add(weather_city2);
		}
//		WeatherServiceDao weatherServiceDao = new WeatherServiceDao(this, this);
//		weatherServiceDao.getWeatherService("深圳", Rcode);
	}

	private void initView() {
		ImageView iv_back = (ImageView) findViewById(R.id.iv_back);
		iv_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		vp_weather = (ViewPager) findViewById(R.id.vp_weather);
		ll_point = (LinearLayout) findViewById(R.id.ll_point);
		WeatherAdapter adapter = new WeatherAdapter(
				getSupportFragmentManager(), list_cityName);
		vp_weather.setAdapter(adapter);
		list.clear();
		ll_point.removeAllViews();
		for (int i = 0; i < CityNum; i++) {
			ImageView iv = new ImageView(mContext);
			LinearLayout.LayoutParams params = new LayoutParams(15, 15);
			params.setMargins(24, 0, 24, 0);
			iv.setLayoutParams(params);
			iv.setImageResource(R.drawable.point_selected);
			list.add(iv);
			ll_point.addView(iv);
		}
		if (list_cityName.size() == 1) {
			ll_point.setVisibility(View.INVISIBLE);
		}
		setPointImageResource(0);

		vp_weather
				.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
					@Override
					public void onPageScrolled(int position,
							float positionOffset, int positionOffsetPixels) {
					}

					@Override
					public void onPageSelected(int position) {
						setPointImageResource(position);
					}

					@Override
					public void onPageScrollStateChanged(int state) {
					}
				});

	}

	protected void setPointImageResource(int position) {
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setImageResource(R.drawable.point_normal);
		}
		list.get(position).setImageResource(R.drawable.point_selected);

	}

}
